﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.Common;
using AspNetCs.Model;
using System.Web;
using System.Web.SessionState;
using System.Collections.Specialized;
using AspNetCs.IDAL;
using System.Reflection;

namespace AspNetCs.Bll
{

    /// <summary>
    /// 用户SESSION或者COOKIES所保存的属性
    /// </summary>
    public class UserSave_Property
    {
        private int _types = StringHelper.isNumber_int(StringHelper.ReadConfig("cors"));//用户类型

        private string _UserCook = "AspNetCs_999qm";
        private string _UserName = "UserName";
        private string _UserId = "UserId";
        private string _Email = "UserEmail";
        private string _Purview = "Purview";

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get { return _UserName; } }
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId { get { return _UserId; } }
        /// <summary>
        /// 用户Email
        /// </summary>
        public string Email { get { return _Email; } }
        /// <summary>
        /// 用户登录的类型,[0是COOKies,1是Session],只读
        /// </summary>
        public int Types { get { return _types; } }
        /// <summary>
        /// 读取用户设置COOKIES的对象名,只读
        /// </summary>
        public string UserCook { get { return _UserCook; } }
        public string Purview { get { return _Purview; } }


    }

    /// <summary>
    /// SESSION相关操作
    /// </summary>
    public class Session_Property : IUserSaveType_Property
    {

        private UserSave_Property property = new UserSave_Property();

        #region IUserSaveType_Property 成员

        #region 属性提取相应值

        /// <summary>
        /// 提取SESSION的用户名
        /// </summary>
        public string GetUserName
        {
            get
            {

                if (HttpContext.Current.Session[property.UserCook + "_" + property.UserName] != null)
                {
                    return HttpContext.Current.Session[property.UserCook + "_" + property.UserName].ToString();
                }
                else
                {
                    return "";
                }


            }
        }

        /// <summary>
        /// 提取SESSION的Email地址
        /// </summary>
        public string GetEmail
        {
            get
            {


                if (HttpContext.Current.Session[property.UserCook + "_" + property.Email] != null)
                {
                    return HttpContext.Current.Session[property.UserCook + "_" + property.Email].ToString();
                }
                else
                {
                    return "";
                }


            }
        }

        /// <summary>
        /// 提取SESSION的用户ID
        /// </summary>
        public int GetUserID
        {
            get
            {

                if (HttpContext.Current.Session[property.UserCook + "_" + property.UserId] != null)
                {
                    return StringHelper.isNumber_int(HttpContext.Current.Session[property.UserCook + "_" + property.UserId].ToString());
                }
                else
                {
                    return 0;
                }

            }
        }


        #endregion


        /// <summary>
        /// 写入用户信息
        /// <param name="ValueArry">写入SESSION的全部键值</param>
        /// <param name="ExpTime">过期时间</param>
        /// </summary>
        public void WriteUserInfo(NameValueCollection ValueArry, int ExpTime)
        {
            foreach (String key in ValueArry.AllKeys)
            {
                HttpContext.Current.Session[property.UserCook + "_" + key] = ValueArry[key].Trim();
            }
            HttpContext.Current.Session.Timeout = ExpTime;
        }
        public void UserExit()
        {
            if (GetUserID != 0)
            {
                new Users().UpdateLastOutTime(" and UserId=" + GetUserID.ToString());
            }
            HttpContext.Current.Session.Clear();
            HttpContext.Current.Session.Abandon();


            //HttpContext.Current.Session.RemoveAll();
            //   UserSave_Property obj = new UserSave_Property();
            //Type t = typeof(UserSave_Property);
            //PropertyInfo[] myPropertyInfo = new UserSave_Property().GetType().GetProperties();
            //for (int i = 0; i < myPropertyInfo.Length; i++)
            //{
            //    if (HttpContext.Current.Session[property.UserCook + "_" + myPropertyInfo[i].GetValue(obj, null).ToString()] != null)
            //    {



            //    }
            //}
        }


        public AspNetCs.Enumerations.UserTypeEnum GetPurview
        {
            get
            {

                int Value = -1;
                if (HttpContext.Current.Session[property.UserCook + "_" + property.Purview] != null)
                {
                    Value = StringHelper.isNumber_int(HttpContext.Current.Session[property.UserCook + "_" + property.Purview].ToString());
                }
                return (AspNetCs.Enumerations.UserTypeEnum)Value;
            }
        }

        #endregion
    }


    /// <summary>
    /// COOKIES相关操作
    /// </summary>
    public class Cookies_Property : IUserSaveType_Property
    {

        private UserSave_Property property = new UserSave_Property();


        #region IUserSaveType_Property 成员

        /// <summary>
        /// 提取COOKIES用户名
        /// </summary>
        public string GetUserName
        {
            get
            {
                string Value = Cookie.GetValue(property.UserCook, property.UserName);
                if (Value == "CookieNonexistence" || Value == "KeyNonexistence")
                {
                    Value = "";
                }
                return Value;
            }
        }
        /// <summary>
        /// 提取COOKIES登录后的用户EMAIL地址
        /// </summary>
        public string GetEmail
        {
            get
            {
                string Value = Cookie.GetValue(property.UserCook, property.Email);
                if (Value == "CookieNonexistence" || Value == "KeyNonexistence")
                {
                    Value = "";
                }
                return Value;
            }
        }

        /// <summary>
        /// 提取COOKIES登录后的用户ID
        /// </summary>
        public int GetUserID
        {
            get
            {
                string Value = Cookie.GetValue(property.UserCook, property.UserId);
                if (Value == "CookieNonexistence" || Value == "KeyNonexistence")
                {
                    Value = "0";
                }

                return StringHelper.isNumber_int(Value);
            }
        }
        /// <summary>
        /// 写入用户信息
        /// <param name="ValueArry">写入COOKIES的全部键值</param>
        /// <param name="ExpTime">过期时间</param>
        /// </summary>
        public void WriteUserInfo(NameValueCollection ValueArry, int ExpTime)
        {

            string _Domain = StringHelper.ReadConfig("Cook_Domain");
            if (_Domain == "")
            {
                Cookie.SetObj(property.UserCook, ExpTime, ValueArry);
            }
            else
            {
                Cookie.SetObj(property.UserCook, ExpTime, ValueArry, _Domain);
            }



        }

        /// <summary>
        /// Cookies退出
        /// </summary>
        public void UserExit()
        {
            if (GetUserID != 0)
            {
                new Users().UpdateLastOutTime(" and UserId=" + GetUserID.ToString());
            }
            Cookie.Del(property.UserCook);
        }

        /// <summary>
        /// 权限{用户，管理员，代理商}
        /// </summary>
        public AspNetCs.Enumerations.UserTypeEnum GetPurview
        {
            get
            {

                string Value = Cookie.GetValue(property.UserCook, property.Purview);
                if (Value == "CookieNonexistence" || Value == "KeyNonexistence")
                {
                    Value = ((int)AspNetCs.Enumerations.UserTypeEnum.Err).ToString();
                }
                return (AspNetCs.Enumerations.UserTypeEnum)StringHelper.isNumber_int(Value);
            }
        }

        #endregion
    }

    public class SaveType_Factory
    {
        private static UserSave_Property Property = new UserSave_Property();
        public static IUserSaveType_Property Create()
        {

            string path = "AspNetCs.Bll";
            string ClassName = "";
            if (Property.Types == 0)
            {
                ClassName = path + ".Cookies_Property";
            }
            else
            {
                ClassName = path + ".Session_Property";
            }
            return (IUserSaveType_Property)Assembly.Load(path).CreateInstance(ClassName);
        }
    }

}
